const db = require('../../config/db.js') //引入数据库
const Sequelize = db.theDb
const projectInfoSchema = Sequelize.import('../../schema/project_info.js');
const pointInfoSchema = Sequelize.import('../../schema/point_info.js');
const r2_groundSchema = Sequelize.import('../../schema/r2_ground.js');
const r2_waterSchema = Sequelize.import('../../schema/r2_water.js');

class AllProjectDataModel {
    static async getAllProjectData(roles, project_owner) {
        let project_owner_list = []
        project_owner.forEach(item => {
            project_owner_list.push({
                project_num: item
            })
        })
        console.log(project_owner_list)
        let AllProjectData
        if (roles.indexOf('admin')) {
            AllProjectData = await projectInfoSchema.findAll()
        } else {
            AllProjectData = await projectInfoSchema.findAll({
                where: {
                    $or: project_owner_list
                }
            })
        }

        return AllProjectData
    }

    static async getPoint(project_num) {
        const pointOptions = await pointInfoSchema.findAll({
            attributes: ['point_num', 'remarks'],
            where: {
                project_num
            }
        })
        return pointOptions
    }

    static async getPointMarkerList(project_num) {
        const markerList = await pointInfoSchema.findAll({
            where: {
                project_num
            }
        })
        return markerList
    }

    static async getElementOptions_list(project_num) {
        const ElementOptions_list = await r2_groundSchema.findOne({
            attributes: { exclude: ['id', 'project_name', 'project_num', 'assess_type'] },
            where: {
                project_num
            }
        })
        return ElementOptions_list
    }


    static async getR2List(selectedBody) {
        let { selectedProject, type } = selectedBody
        let r2List
        if(type == 'ground'){
            r2List = await r2_groundSchema.findAll({
                where: {
                    project_num: selectedProject
                }
            })
        }else {
            r2List = await r2_waterSchema.findAll({
                where: {
                    project_num: selectedProject
                }
            })
        }
        return r2List
    }
}

module.exports = AllProjectDataModel